package itsecurity.cert;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Security;
import java.security.cert.CertificateEncodingException;

import iaik.security.provider.IAIK;
import iaik.x509.X509Certificate;

/**
 * Handles the saving of certificates to files
 *
 */
public class SaveCertToFile {

	public static void saveCertificate(X509Certificate serti, String fileName){
		try {
			FileOutputStream fos = new FileOutputStream(fileName);
			try {
				fos.write(serti.getEncoded());
				fos.close();
			} catch (CertificateEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	/**
	 * Just to save some certs manually
	 * @param args
	 */
	public static void main (String []args) {
		try {
	    Security.insertProviderAt(new IAIK(), 2);
	    Security.insertProviderAt(new MDBProvider(), 3);
	  } catch( Throwable e ) {
	    //
	  }
		saveCertificate((X509Certificate)KeyManager.getCert("Circus Authorities", "Bob".toCharArray(), new File(KeyManager.KEYSTORE)),"CACert.cer");
	}
}
